package com.videogo;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.csun.nusing.App;
import com.videogo.constant.Config;
import com.videogo.constant.Constant;
import com.videogo.constant.IntentConsts;
import com.videogo.errorlayer.ErrorInfo;
import com.videogo.exception.BaseException;
import com.videogo.exception.ErrorCode;
import com.videogo.exception.InnerException;
import com.videogo.openapi.EZConstants.EZRealPlayConstants;
import com.videogo.openapi.EZConstants.EZVideoLevel;
import com.videogo.openapi.EZOpenSDKListener;
import com.videogo.openapi.EZPlayer;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;
import com.videogo.playbacklist.ScreenOrientationHelper;
import com.videogo.realplay.RealPlayStatus;
import com.videogo.util.AudioPlayUtil;
import com.videogo.util.ConnectionDetector;
import com.videogo.util.DataManager;
import com.videogo.util.EZUtils;
import com.videogo.util.LoadingTextView;
import com.videogo.util.LocalInfo;
import com.videogo.util.LogUtil;
import com.videogo.util.MediaScanner;
import com.videogo.util.RealPlaySquareInfo;
import com.videogo.util.RotateViewUtil;
import com.videogo.util.SDCardUtil;
import com.videogo.util.SPUtils;
import com.videogo.util.Utils;
import com.videogo.util.VerifyCodeInput;
import com.videogo.util.WaitDialog;
import com.videogo.widget.CheckTextButton;
import com.videogo.widget.CustomRect;
import com.videogo.widget.CustomTouchListener;
import com.videogo.widget.TitleBar;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import com.csun.nusing.R;

/**
 * 实时预览2.7
 * 
 * @author xiaxingsuo
 * @data 2015-11-11
 */
public class VideoPlayerActivity extends Activity implements OnClickListener,
		SurfaceHolder.Callback, Handler.Callback, OnTouchListener,
		com.videogo.util.VerifyCodeInput.VerifyCodeInputListener {
	private static final String TAG = "VideoPlayerActivity";
	/**
	 * 动画时间
	 */
	private static final int ANIMATION_DURING_TIME = 500;

	public static final float BIG_SCREEN_RATIO = 1.60f;

	// UI消息
	public static final int MSG_PLAY_UI_UPDATE = 200;

	public static final int MSG_AUTO_START_PLAY = 202;

	public static final int MSG_CLOSE_PTZ_PROMPT = 203;

	public static final int MSG_HIDE_PTZ_DIRECTION = 204;

	public static final int MSG_HIDE_PAGE_ANIM = 205;

	public static final int MSG_PLAY_UI_REFRESH = 206;

	public static final int MSG_PREVIEW_START_PLAY = 207;

	public static final int MSG_SET_VEDIOMODE_SUCCESS = 105;

	/**
	 * 设置视频质量成功
	 */
	public static final int MSG_SET_VEDIOMODE_FAIL = 106;

	// 视频广场URL
	private String mRtspUrl = null;
	// 视频广场播放信息
	private RealPlaySquareInfo mRealPlaySquareInfo = null;
	// private EZCameraInfo mCameraInfo = null;

	private AudioPlayUtil mAudioPlayUtil = null;
	private LocalInfo mLocalInfo = null;
	private Handler mHandler = null;

	private float mRealRatio = Constant.LIVE_VIEW_RATIO;
	/**
	 * 标识是否正在播放
	 */
	private int mStatus = RealPlayStatus.STATUS_INIT;
	private boolean mIsOnStop = false;
	/**
	 * 屏幕当前方向
	 */
	private int mOrientation = Configuration.ORIENTATION_PORTRAIT;
	private int mForceOrientation = 0;
	private Rect mRealPlayRect = null;

	private LinearLayout mRealPlayPageLy = null;
	private TitleBar mPortraitTitleBar = null;
	private TitleBar mLandscapeTitleBar = null;
	private Button mTiletRightBtn = null;
	private RelativeLayout mRealPlayPlayRl = null;

	private SurfaceView mRealPlaySv = null;
	private SurfaceHolder mRealPlaySh = null;
	private CustomTouchListener mRealPlayTouchListener = null;

	// 提示布局界面
	private RelativeLayout mRealPlayPromptRl = null;
	// loading控件
	private RelativeLayout mRealPlayLoadingRl;
	private TextView mRealPlayTipTv;
	private ImageView mRealPlayPlayIv;
	private LoadingTextView mRealPlayPlayLoading;
	private LinearLayout mRealPlayPlayPrivacyLy;
	// 隐私保护动画
	private ImageView mPageAnimIv = null;
	private AnimationDrawable mPageAnimDrawable = null;

	private LinearLayout mRealPlayControlRl = null;
	private ImageButton mRealPlayBtn = null;
	private ImageButton mRealPlaySoundBtn = null;
	private TextView mRealPlayFlowTv = null;
	private int mControlDisplaySec = 0;

	// 播放比例
	private float mPlayScale = 1;

	private RelativeLayout mRealPlayCaptureRl = null;
	private LayoutParams mRealPlayCaptureRlLp = null;
	private ImageView mRealPlayCaptureIv = null;
	private ImageView mRealPlayCaptureWatermarkIv = null;
	private int mCaptureDisplaySec = 0;
	private LinearLayout mRealPlayRecordLy = null;
	private ImageView mRealPlayRecordIv = null;
	private TextView mRealPlayRecordTv = null;

	private boolean mIsRecording = false;
	private String mRecordTime = null;
	/**
	 * 录像时间
	 */
	private int mRecordSecond = 0;

	private HorizontalScrollView mRealPlayOperateBar = null;

	private LinearLayout mRealPlaySslBtnLy = null;
	// private LinearLayout mRealPlayPrivacyBtnLy = null;
	private LinearLayout mRealPlayCaptureBtnLy = null;
	private LinearLayout mRealPlayRecordContainerLy = null;

	private Button mRealPlaySslBtn = null;
	private ImageButton mRealPlayPrivacyBtn = null;
	private ImageButton mRealPlayCaptureBtn = null;
	private View mRealPlayRecordContainer = null;

	private ImageButton mRealPlayRecordBtn = null;
	private ImageButton mRealPlayRecordStartBtn = null;
	private RotateViewUtil mRecordRotateViewUtil = null;

	private Button mRealPlayQualityBtn = null;

	private RelativeLayout mRealPlayFullOperateBar = null;
	private ImageButton mRealPlayFullPlayBtn = null;
	private ImageButton mRealPlayFullSoundBtn = null;
	private ImageButton mRealPlayFullCaptureBtn = null;
	private ImageButton mRealPlayFullRecordBtn = null;
	private ImageButton mRealPlayFullRecordStartBtn = null;
	private View mRealPlayFullRecordContainer = null;
	private LinearLayout mRealPlayFullFlowLy = null;
	private TextView mRealPlayFullRateTv = null;
	private TextView mRealPlayFullFlowTv = null;
	private TextView mRealPlayRatioTv = null;
	private ImageButton mRealPlayFullAnimBtn = null;
	private int[] mStartXy = new int[2];
	private int[] mEndXy = new int[2];
	private PopupWindow mQualityPopupWindow = null;
	private WaitDialog mWaitDialog = null;

	/**
	 * 监听锁屏解锁的事件
	 */
	private RealPlayBroadcastReceiver mBroadcastReceiver = null;
	/**
	 * 定时器
	 */
	private Timer mUpdateTimer = null;
	/**
	 * 定时器执行的任务
	 */
	private TimerTask mUpdateTimerTask = null;

	// 全屏按钮
	private CheckTextButton mFullscreenButton;
	private CheckTextButton mFullscreenFullButton;
	private ScreenOrientationHelper mScreenOrientationHelper;

	// 弱提示预览信息
	private long mStartTime = 0;
	private long mStopTime = 0;

	// 云台控制状态
	private float mZoomScale = 0;
	private int mCommand = -1;

	// 横屏对讲
	// 直播预告
	private TextView mRealPlayPreviewTv = null;

	/**
	 * 演示点预览控制对象
	 */
	private EZPlayer mEZPlayer = null;
	// private StubPlayer mStub = new StubPlayer();
	private CheckTextButton mFullScreenTitleBarBackBtn;
	private EZVideoLevel mCurrentQulityMode = EZVideoLevel.VIDEO_LEVEL_HD;
	private EZDeviceInfo mDeviceInfo = null;
	private EZCameraInfo mCameraInfo = null;
	private String mVerifyCode;

	/**
	 * 存放上一次的流量
	 */
	private long mStreamFlow = 0;

	/**
	 * 当前流量
	 */
	private int mRealFlow = 0;
	private RelativeLayout titlerl;

	/**
	 * ATTENTION: This was auto-generated to implement the App Indexing API. See
	 * https://g.co/AppIndexing/AndroidStudio for more information.
	 */
	// private GoogleApiClient client;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initData();
		initView();

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
			return;
		}
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				if (mRealPlaySv != null) {
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(mRealPlaySv.getWindowToken(), 0);
				}
			}
		}, 200);

		initUI();
		mRealPlaySv.setVisibility(View.VISIBLE);

		LogUtil.infoLog(TAG, "onResume real play status:" + mStatus);
		if (mCameraInfo != null && mDeviceInfo != null
				&& mDeviceInfo.getStatus() != 1) {
			if (mStatus != RealPlayStatus.STATUS_STOP) {
				stopRealPlay();
			}
			setRealPlayFailUI(getString(R.string.realplay_fail_device_not_exist));
		} else {
			if (mStatus == RealPlayStatus.STATUS_INIT
					|| mStatus == RealPlayStatus.STATUS_PAUSE
					|| mStatus == RealPlayStatus.STATUS_DECRYPT) {
				// 开始播放
				startRealPlay();
			}
		}
		mIsOnStop = false;
	}

	@Override
	protected void onStart() {
		super.onStart();

	}

	@Override
	protected void onStop() {
		super.onStop();

		if (mScreenOrientationHelper != null) {
			mScreenOrientationHelper.postOnStop();
		}

		mHandler.removeMessages(MSG_AUTO_START_PLAY);
		hidePageAnim();

		if (mCameraInfo == null && mRtspUrl == null) {
			return;
		}

		if (mStatus != RealPlayStatus.STATUS_STOP) {
			mIsOnStop = true;
			stopRealPlay();
			mStatus = RealPlayStatus.STATUS_PAUSE;
			setRealPlayStopUI();
		} else {
			setStopLoading();
		}
		mRealPlaySv.setVisibility(View.INVISIBLE);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (mEZPlayer != null) {
			mEZPlayer.release();

		}

		mHandler.removeMessages(MSG_AUTO_START_PLAY);
		mHandler.removeMessages(MSG_HIDE_PTZ_DIRECTION);
		mHandler.removeMessages(MSG_CLOSE_PTZ_PROMPT);
		mHandler.removeMessages(MSG_HIDE_PAGE_ANIM);
		mHandler = null;

		if (mBroadcastReceiver != null) {
			// 取消锁屏广播的注册
			unregisterReceiver(mBroadcastReceiver);
			mBroadcastReceiver = null;
		}

		mScreenOrientationHelper = null;
	}

	private void exit() {
		if (mStatus != RealPlayStatus.STATUS_STOP) {
			stopRealPlay();
			setRealPlayStopUI();
		}
		mHandler.removeMessages(MSG_AUTO_START_PLAY);
		mHandler.removeMessages(MSG_HIDE_PTZ_DIRECTION);
		mHandler.removeMessages(MSG_CLOSE_PTZ_PROMPT);
		mHandler.removeMessages(MSG_HIDE_PAGE_ANIM);
		if (mBroadcastReceiver != null) {
			// 取消锁屏广播的注册
			unregisterReceiver(mBroadcastReceiver);
			mBroadcastReceiver = null;
		}
		finish();
	}

	@Override
	public void finish() {
		if (mCameraInfo != null) {
			Intent intent = new Intent();
			intent.putExtra(IntentConsts.EXTRA_DEVICE_ID,
					mCameraInfo.getDeviceSerial());
			intent.putExtra(IntentConsts.EXTRA_CAMERA_NO,
					mCameraInfo.getCameraNo());
			intent.putExtra("video_level", mCameraInfo.getVideoLevel()
					.getVideoLevel());
			setResult(VideoListActivity.RESULT_CODE, intent);
		}
		super.finish();
	}

	@Override
	public void onBackPressed() {
		if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {
			mScreenOrientationHelper.portrait();
			return;
		}
		exit();
	}

	// 初始化数据对象
	private void initData() {
		// 获取本地信息
		Application application = (Application) getApplication();
		mAudioPlayUtil = AudioPlayUtil.getInstance(application);
		// 获取配置信息操作对象
		mLocalInfo = LocalInfo.getInstance();
		// 获取屏幕参数
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		mLocalInfo
				.setScreenWidthHeight(metric.widthPixels, metric.heightPixels);
		mLocalInfo.setNavigationBarHeight((int) Math.ceil(25 * getResources()
				.getDisplayMetrics().density));

		mHandler = new Handler(this);
		mRecordRotateViewUtil = new RotateViewUtil();

		mBroadcastReceiver = new RealPlayBroadcastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_USER_PRESENT);
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		registerReceiver(mBroadcastReceiver, filter);

		mRealPlaySquareInfo = new RealPlaySquareInfo();
		Intent intent = getIntent();
		if (intent != null) {
			mCameraInfo = intent
					.getParcelableExtra(IntentConsts.EXTRA_CAMERA_INFO);
			mDeviceInfo = intent
					.getParcelableExtra(IntentConsts.EXTRA_DEVICE_INFO);
			mRtspUrl = intent.getStringExtra(IntentConsts.EXTRA_RTSP_URL);
			if (mCameraInfo != null) {
				mCurrentQulityMode = (mCameraInfo.getVideoLevel());
			}
			LogUtil.debugLog(TAG, "rtspUrl:" + mRtspUrl);

			getRealPlaySquareInfo();
		}
		if (mDeviceInfo != null && mDeviceInfo.getIsEncrypt() == 1) {
			mVerifyCode = DataManager.getInstance().getDeviceSerialVerifyCode(
					mCameraInfo.getDeviceSerial());
		}
	}

	private void getRealPlaySquareInfo() {
		if (TextUtils.isEmpty(mRtspUrl)) {
			return;
		}

		Uri uri = Uri.parse(mRtspUrl.replaceFirst("&", "?"));
		try {
			mRealPlaySquareInfo.mSquareId = Integer.parseInt(uri
					.getQueryParameter("squareid"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		try {
			mRealPlaySquareInfo.mChannelNo = Integer.parseInt(Utils
					.getUrlValue(mRtspUrl, "channelno=", "&"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		mRealPlaySquareInfo.mCameraName = uri.getQueryParameter("cameraname");
		try {
			mRealPlaySquareInfo.mSoundType = Integer.parseInt(uri
					.getQueryParameter("soundtype"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		mRealPlaySquareInfo.mCoverUrl = uri.getQueryParameter("md5Serial");
		if (!TextUtils.isEmpty(mRealPlaySquareInfo.mCoverUrl)) {
			mRealPlaySquareInfo.mCoverUrl = mLocalInfo.getServAddr()
					+ mRealPlaySquareInfo.mCoverUrl + "_mobile.jpeg";
		}
	}

	/**
	 * screen状态广播接收者
	 */
	private class RealPlayBroadcastReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
				if (mStatus != RealPlayStatus.STATUS_STOP) {
					stopRealPlay();
					mStatus = RealPlayStatus.STATUS_PAUSE;
					setRealPlayStopUI();
				}
			}
		}
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	private void initTitleBar() {
		mPortraitTitleBar = (TitleBar) findViewById(R.id.title_bar_portrait);
		mPortraitTitleBar.addLeftButton(R.drawable.back,new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mStatus != RealPlayStatus.STATUS_STOP) {
					stopRealPlay();
					setRealPlayStopUI();
				}
				finish();
			}
		});
		mLandscapeTitleBar = (TitleBar) findViewById(R.id.title_bar_landscape);
		mLandscapeTitleBar.setStyle(Color.rgb(0xff, 0xff, 0xff), getResources()
				.getDrawable(R.color.dark_bg_70p),
				getResources().getDrawable(R.drawable.message_back_selector));
		mLandscapeTitleBar.setOnTouchListener(this);
		mFullScreenTitleBarBackBtn = new CheckTextButton(this);
		mFullScreenTitleBarBackBtn.setBackground(getResources().getDrawable(
				R.drawable.common_title_back_selector));
		mLandscapeTitleBar.addLeftView(mFullScreenTitleBarBackBtn);
	}

	private void initRealPlayPageLy() {
		mRealPlayPageLy = (LinearLayout) findViewById(R.id.realplay_page_ly);
		/** 测量状态栏高度 **/
		ViewTreeObserver viewTreeObserver = mRealPlayPageLy
				.getViewTreeObserver();
		viewTreeObserver
				.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						if (mRealPlayRect == null) {
							// 获取状况栏高度
							mRealPlayRect = new Rect();
							getWindow()
									.getDecorView()
									.getWindowVisibleDisplayFrame(mRealPlayRect);
						}
					}
				});
	}

	// 初始化界面
	private void initView() {
		setContentView(R.layout.ez_realplay_page);
		// 保持屏幕常亮
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		initTitleBar();
		initRealPlayPageLy();
		initLoadingUI();

		mRealPlayPlayRl = (RelativeLayout) findViewById(R.id.realplay_play_rl);
		mRealPlaySv = (SurfaceView) findViewById(R.id.realplay_sv);
		mRealPlaySh = mRealPlaySv.getHolder();
		mRealPlaySh.addCallback(this);
		mRealPlayTouchListener = new CustomTouchListener() {

			@Override
			public boolean canZoom(float scale) {
				if (mStatus == RealPlayStatus.STATUS_PLAY) {
					return true;
				} else {
					return false;
				}
			}

			@Override
			public boolean canDrag(int direction) {
				if (mStatus != RealPlayStatus.STATUS_PLAY) {
					return false;
				}
				if (mEZPlayer != null && mDeviceInfo != null) {
					// 出界判断
					if (DRAG_LEFT == direction || DRAG_RIGHT == direction) {
						// 左移/右移出界判断
						if (mDeviceInfo.isSupportPTZ()) {
							return true;
						}
					} else if (DRAG_UP == direction || DRAG_DOWN == direction) {
						// 上移/下移出界判断
						if (mDeviceInfo.isSupportPTZ()) {
							return true;
						}
					}
				}
				return false;
			}

			@Override
			public void onSingleClick() {
				onRealPlaySvClick();
			}

			@Override
			public void onDoubleClick(MotionEvent e) {
			}

			@Override
			public void onZoom(float scale) {
				LogUtil.debugLog(TAG, "onZoom:" + scale);
				if (mEZPlayer != null && mDeviceInfo != null
						&& mDeviceInfo.isSupportZoom()) {
					startZoom(scale);
				}
			}

			@Override
			public void onDrag(int direction, float distance, float rate) {
				LogUtil.debugLog(TAG, "onDrag:" + direction);
				if (mEZPlayer != null) {
					// Utils.showLog(RealPlayActivity.this, "onDrag rate:" +
					// rate);
					startDrag(direction, distance, rate);
				}
			}

			@Override
			public void onEnd(int mode) {
				LogUtil.debugLog(TAG, "onEnd:" + mode);
				if (mEZPlayer != null) {
					stopDrag(false);
				}
				if (mEZPlayer != null && mDeviceInfo != null
						&& mDeviceInfo.isSupportZoom()) {
					stopZoom();
				}
			}

			@Override
			public void onZoomChange(float scale, CustomRect oRect,
					CustomRect curRect) {
				LogUtil.debugLog(TAG, "onZoomChange:" + scale);
				if (mEZPlayer != null && mDeviceInfo != null
						&& mDeviceInfo.isSupportZoom()) {
					// 采用云台调焦
					return;
				}
				if (mStatus == RealPlayStatus.STATUS_PLAY) {
					if (scale > 1.0f && scale < 1.1f) {
						scale = 1.1f;
					}
					setPlayScaleUI(scale, oRect, curRect);
				}
			}
		};
		mRealPlaySv.setOnTouchListener(mRealPlayTouchListener);
		mRealPlayPromptRl = (RelativeLayout) findViewById(R.id.realplay_prompt_rl);
		mRealPlayControlRl = (LinearLayout) findViewById(R.id.realplay_control_rl);
		mRealPlayBtn = (ImageButton) findViewById(R.id.realplay_play_btn);
		mRealPlaySoundBtn = (ImageButton) findViewById(R.id.realplay_sound_btn);
		mRealPlayFlowTv = (TextView) findViewById(R.id.realplay_flow_tv);
		mRealPlayFlowTv.setText("0k/s");

		mRealPlayCaptureRl = (RelativeLayout) findViewById(R.id.realplay_capture_rl);
		mRealPlayCaptureRlLp = (LayoutParams) mRealPlayCaptureRl
				.getLayoutParams();
		mRealPlayCaptureIv = (ImageView) findViewById(R.id.realplay_capture_iv);
		mRealPlayCaptureWatermarkIv = (ImageView) findViewById(R.id.realplay_capture_watermark_iv);
		mRealPlayRecordLy = (LinearLayout) findViewById(R.id.realplay_record_ly);
		mRealPlayRecordIv = (ImageView) findViewById(R.id.realplay_record_iv);
		mRealPlayRecordTv = (TextView) findViewById(R.id.realplay_record_tv);

		mRealPlayQualityBtn = (Button) findViewById(R.id.realplay_quality_btn);

		mRealPlayFullFlowLy = (LinearLayout) findViewById(R.id.realplay_full_flow_ly);
		mRealPlayFullRateTv = (TextView) findViewById(R.id.realplay_full_rate_tv);
		mRealPlayFullFlowTv = (TextView) findViewById(R.id.realplay_full_flow_tv);
		mRealPlayRatioTv = (TextView) findViewById(R.id.realplay_ratio_tv);
		mRealPlayFullRateTv.setText("0k/s");
		mRealPlayFullFlowTv.setText("0MB");

		mFullscreenButton = (CheckTextButton) findViewById(R.id.fullscreen_button);
		mFullscreenFullButton = (CheckTextButton) findViewById(R.id.fullscreen_full_button);

		if (mRtspUrl == null) {
			initOperateBarUI();

			initFullOperateBarUI();

			// mj LinearLayout.LayoutParams realPlayPlayRlLp = new
			// LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
			// LayoutParams.WRAP_CONTENT);
			// realPlayPlayRlLp.gravity = Gravity.CENTER;
			// mRealPlayPlayRl.setLayoutParams(realPlayPlayRlLp);
			mRealPlayOperateBar.setVisibility(View.VISIBLE);
		} else {
			// mRealPlayPageLy.setBackgroundColor(getResources().getColor(R.color.black_bg));
			LinearLayout.LayoutParams realPlayPlayRlLp = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			realPlayPlayRlLp.gravity = Gravity.CENTER;
			// mj 2015/11/01 realPlayPlayRlLp.weight = 1;
			mRealPlayPlayRl.setLayoutParams(realPlayPlayRlLp);
			mRealPlayPlayRl.setBackgroundColor(getResources().getColor(
					R.color.common_bg));
		}

		setRealPlaySvLayout();
		initCaptureUI();
		mScreenOrientationHelper = new ScreenOrientationHelper(this,
				mFullscreenButton, /* mFullscreenFullButton */
				mFullScreenTitleBarBackBtn);

		mWaitDialog = new WaitDialog(this,
				android.R.style.Theme_Translucent_NoTitleBar);
		mWaitDialog.setCancelable(false);
	}

	public void startDrag(int direction, float distance, float rate) {
	}

	public void stopDrag(boolean control) {
	}

	private void startZoom(float scale) {
		if (mEZPlayer == null) {
			return;
		}

		hideControlRlAndFullOperateBar(false);
		boolean preZoomIn = mZoomScale > 1.01 ? true : false;
		boolean zoomIn = scale > 1.01 ? true : false;
		if (mZoomScale != 0 && preZoomIn != zoomIn) {
			LogUtil.debugLog(TAG, "startZoom stop:" + mZoomScale);
			// mEZOpenSDK.controlPTZ(mZoomScale > 1.01 ?
			// RealPlayStatus.PTZ_ZOOMIN
			// : RealPlayStatus.PTZ_ZOOMOUT, RealPlayStatus.PTZ_SPEED_DEFAULT,
			// EZPlayer.PTZ_COMMAND_STOP);
			mZoomScale = 0;
		}
		if (scale != 0 && (mZoomScale == 0 || preZoomIn != zoomIn)) {
			mZoomScale = scale;
			LogUtil.debugLog(TAG, "startZoom start:" + mZoomScale);
			// mEZOpenSDK.controlPTZ(mZoomScale > 1.01 ?
			// RealPlayStatus.PTZ_ZOOMIN
			// : RealPlayStatus.PTZ_ZOOMOUT, RealPlayStatus.PTZ_SPEED_DEFAULT,
			// EZPlayer.PTZ_COMMAND_START);
		}
	}

	private void stopZoom() {
		if (mEZPlayer == null) {
			return;
		}
		if (mZoomScale != 0) {
			LogUtil.debugLog(TAG, "stopZoom stop:" + mZoomScale);
			// mEZOpenSDK.controlPTZ(mZoomScale > 1.01 ?
			// RealPlayStatus.PTZ_ZOOMIN
			// : RealPlayStatus.PTZ_ZOOMOUT, RealPlayStatus.PTZ_SPEED_DEFAULT,
			// EZPlayer.PTZ_COMMAND_STOP);
			mZoomScale = 0;
		}
	}

	private int getSupportPtz() {
		if (mEZPlayer == null || mDeviceInfo == null) {
			return 0;
		}

		if (mDeviceInfo.isSupportPTZ() || mDeviceInfo.isSupportZoom()) {
			return 1;
		} else {
			return 0;
		}
	}

	// 初始化UI
	@SuppressWarnings("deprecation")
	private void initUI() {
		mPageAnimDrawable = null;
		mRealPlaySoundBtn.setVisibility(View.VISIBLE);

		if (mCameraInfo != null) {
			mPortraitTitleBar.setTitle(mCameraInfo.getCameraName());
			mLandscapeTitleBar.setTitle(mCameraInfo.getCameraName());

			setCameraInfoTiletRightBtn();

			if (mLocalInfo.isSoundOpen()) {
				mRealPlaySoundBtn
						.setBackgroundResource(R.drawable.ezopen_vertical_preview_sound_selector);
				mRealPlayFullSoundBtn
						.setBackgroundResource(R.drawable.play_full_soundon_btn_selector);
			} else {
				mRealPlaySoundBtn
						.setBackgroundResource(R.drawable.ezopen_vertical_preview_sound_off_selector);
				mRealPlayFullSoundBtn
						.setBackgroundResource(R.drawable.play_full_soundoff_btn_selector);
			}

			mRealPlayCaptureBtnLy.setVisibility(View.VISIBLE);
			mRealPlayFullCaptureBtn.setVisibility(View.VISIBLE);
			mRealPlayRecordContainerLy.setVisibility(View.VISIBLE);
			mRealPlayFullRecordContainer.setVisibility(View.VISIBLE);
			mRealPlayQualityBtn.setVisibility(View.VISIBLE);
			mRealPlayFullSoundBtn.setVisibility(View.VISIBLE);

			updateUI();
		} else if (mRtspUrl != null) {
			if (!TextUtils.isEmpty(mRealPlaySquareInfo.mCameraName)) {
				mPortraitTitleBar.setTitle(mRealPlaySquareInfo.mCameraName);
				mLandscapeTitleBar.setTitle(mRealPlaySquareInfo.mCameraName);
			}
			mRealPlaySoundBtn.setVisibility(View.GONE);
			mRealPlayQualityBtn.setVisibility(View.GONE);
		}

		if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
			updateOperatorUI();
		}
	}

	private void setCameraInfoTiletRightBtn() {
		if (mTiletRightBtn != null && mDeviceInfo != null) {
			if (mDeviceInfo.getStatus() == 1) {
				mTiletRightBtn.setVisibility(View.VISIBLE);
			} else {
				mTiletRightBtn.setVisibility(View.GONE);
			}
		}
	}

	private void initOperateBarUI() {
		if (mRealPlayOperateBar != null) {
			mRealPlayOperateBar.setVisibility(View.GONE);
			mRealPlayOperateBar = null;
		}
		mRealPlayOperateBar = (HorizontalScrollView) findViewById(R.id.ezopen_realplay_operate_bar);
		mRealPlaySslBtnLy = (LinearLayout) findViewById(R.id.realplay_ssl_btn_ly);
		mRealPlayCaptureBtnLy = (LinearLayout) findViewById(R.id.realplay_previously_btn_ly);
		mRealPlayRecordContainerLy = (LinearLayout) findViewById(R.id.realplay_video_container_ly);
		mRealPlaySslBtn = (Button) findViewById(R.id.realplay_ssl_btn);
		mRealPlayPrivacyBtn = (ImageButton) findViewById(R.id.realplay_privacy_btn);
		mRealPlayCaptureBtn = (ImageButton) findViewById(R.id.realplay_previously_btn);
		mRealPlayRecordContainer = findViewById(R.id.realplay_video_container);
		mRealPlayRecordBtn = (ImageButton) findViewById(R.id.realplay_video_btn);
		mRealPlayRecordStartBtn = (ImageButton) findViewById(R.id.realplay_video_start_btn);
		mRealPlayOperateBar.setVisibility(View.VISIBLE);
	}

	private void setBigScreenOperateBtnLayout() {
	}

	private void initFullOperateBarUI() {
		mRealPlayFullOperateBar = (RelativeLayout) findViewById(R.id.realplay_full_operate_bar);
		mRealPlayFullPlayBtn = (ImageButton) findViewById(R.id.realplay_full_play_btn);
		mRealPlayFullSoundBtn = (ImageButton) findViewById(R.id.realplay_full_sound_btn);
		mRealPlayFullCaptureBtn = (ImageButton) findViewById(R.id.realplay_full_previously_btn);
		mRealPlayFullRecordContainer = findViewById(R.id.realplay_full_video_container);
		mRealPlayFullRecordBtn = (ImageButton) findViewById(R.id.realplay_full_video_btn);
		mRealPlayFullRecordStartBtn = (ImageButton) findViewById(R.id.realplay_full_video_start_btn);
		mRealPlayFullOperateBar.setOnTouchListener(this);
		mRealPlayFullAnimBtn = (ImageButton) findViewById(R.id.realplay_full_anim_btn);
	}

	private void startFullBtnAnim(final View animView, final int[] startXy,
			final int[] endXy, final AnimationListener animationListener) {
		animView.setVisibility(View.VISIBLE);
		TranslateAnimation anim = new TranslateAnimation(startXy[0], endXy[0],
				startXy[1], endXy[1]);
		anim.setAnimationListener(animationListener);
		anim.setDuration(ANIMATION_DURING_TIME);
		animView.startAnimation(anim);
	}

	private void setVideoLevel() {
		if (mCameraInfo == null || mEZPlayer == null || mDeviceInfo == null) {
			return;
		}

		if (mDeviceInfo.getStatus() == 1) {
			mRealPlayQualityBtn.setEnabled(true);
		} else {
			mRealPlayQualityBtn.setEnabled(false);
		}

		/************** 本地数据保存 需要更新之前获取到的设备列表信息，开发者自己设置 *********************/
		mCameraInfo.setVideoLevel(mCurrentQulityMode.getVideoLevel());

		// 视频质量，2-高清，1-标清，0-流畅
		if (mCurrentQulityMode.getVideoLevel() == EZVideoLevel.VIDEO_LEVEL_FLUNET
				.getVideoLevel()) {
			mRealPlayQualityBtn.setText(R.string.quality_flunet);
		} else if (mCurrentQulityMode.getVideoLevel() == EZVideoLevel.VIDEO_LEVEL_BALANCED
				.getVideoLevel()) {
			mRealPlayQualityBtn.setText(R.string.quality_balanced);
		} else if (mCurrentQulityMode.getVideoLevel() == EZVideoLevel.VIDEO_LEVEL_HD
				.getVideoLevel()) {
			mRealPlayQualityBtn.setText(R.string.quality_hd);
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		mOrientation = newConfig.orientation;

		onOrientationChanged();
		super.onConfigurationChanged(newConfig);
	}

	private void updateOrientation() {
		if (mStatus == RealPlayStatus.STATUS_PLAY) {
			setOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		} else {
			if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
				setOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			} else {
				setOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
			}
		}
	}

	private void updateOperatorUI() {
		if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
			// 显示状态栏
			fullScreen(false);
			updateOrientation();
			mPortraitTitleBar.setVisibility(View.VISIBLE);
			mLandscapeTitleBar.setVisibility(View.GONE);
			mRealPlayControlRl.setVisibility(View.VISIBLE);
			if (mRtspUrl == null) {
				mRealPlayPageLy.setBackgroundColor(getResources().getColor(
						R.color.common_bg));
				mRealPlayOperateBar.setVisibility(View.VISIBLE);
				mRealPlayFullOperateBar.setVisibility(View.GONE);
				mFullscreenFullButton.setVisibility(View.GONE);
				if (mIsRecording) {
					mRealPlayRecordBtn.setVisibility(View.GONE);
					mRealPlayRecordStartBtn.setVisibility(View.VISIBLE);
				} else {
					mRealPlayRecordBtn.setVisibility(View.VISIBLE);
					mRealPlayRecordStartBtn.setVisibility(View.GONE);
				}
			}
		} else {
			// 隐藏状态栏
			fullScreen(true);
			mPortraitTitleBar.setVisibility(View.GONE);
			// hide the
			mRealPlayControlRl.setVisibility(View.GONE);
			if (mRtspUrl == null) {
				mRealPlayOperateBar.setVisibility(View.GONE);
				mRealPlayPageLy.setBackgroundColor(getResources().getColor(
						R.color.black_bg));
				mRealPlayFullOperateBar.setVisibility(View.GONE);
				if (mIsRecording) {
					mRealPlayFullRecordBtn.setVisibility(View.GONE);
					mRealPlayFullRecordStartBtn.setVisibility(View.VISIBLE);
				} else {
					mRealPlayFullRecordBtn.setVisibility(View.VISIBLE);
					mRealPlayFullRecordStartBtn.setVisibility(View.GONE);
				}
			}
		}

		// mRealPlayControlRl.setVisibility(View.GONE);
		closeQualityPopupWindow();
		if (mStatus == RealPlayStatus.STATUS_START) {
			showControlRlAndFullOperateBar();
		}
	}

	private void fullScreen(boolean enable) {
		if (enable) {
			WindowManager.LayoutParams lp = getWindow().getAttributes();
			lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
			getWindow().setAttributes(lp);
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
		} else {
			WindowManager.LayoutParams attr = getWindow().getAttributes();
			attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
			getWindow().setAttributes(attr);
			getWindow().clearFlags(
					WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
		}
	}

	private void onOrientationChanged() {
		// mRealPlaySv.setVisibility(View.INVISIBLE);
		setRealPlaySvLayout();
		// mRealPlaySv.setVisibility(View.VISIBLE);

		updateOperatorUI();
		updateCaptureUI();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder
	 * , int, int, int)
	 */
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder
	 * )
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if (mEZPlayer != null) {
			mEZPlayer.setSurfaceHold(holder);
		}
		mRealPlaySh = holder;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.
	 * SurfaceHolder)
	 */
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (mEZPlayer != null) {
			mEZPlayer.setSurfaceHold(null);
		}
		mRealPlaySh = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.realplay_play_btn:
		case R.id.realplay_full_play_btn:
		case R.id.realplay_play_iv:
			if (mStatus != RealPlayStatus.STATUS_STOP) {
				stopRealPlay();
				setRealPlayStopUI();
			} else {
				startRealPlay();
			}
			break;
		case R.id.realplay_previously_btn:
		case R.id.realplay_previously_btn2:
		case R.id.realplay_full_previously_btn:
			onCapturePicBtnClick();
			break;
		case R.id.realplay_capture_rl:
			onCaptureRlClick();
			break;
		case R.id.realplay_video_btn:
		case R.id.realplay_video_start_btn:
		case R.id.realplay_video_btn2:
		case R.id.realplay_video_start_btn2:
		case R.id.realplay_full_video_btn:
		case R.id.realplay_full_video_start_btn:
			onRecordBtnClick();
			break;
		case R.id.realplay_quality_btn:
			openQualityPopupWindow(mRealPlayQualityBtn);
			break;
		case R.id.realplay_sound_btn:
		case R.id.realplay_full_sound_btn:
			onSoundBtnClick();
			break;
		default:
			break;
		}
	}

	private void onSoundBtnClick() {
		if (mLocalInfo.isSoundOpen()) {
			mLocalInfo.setSoundOpen(false);
			mRealPlaySoundBtn
					.setBackgroundResource(R.drawable.ezopen_vertical_preview_sound_off_selector);
			if (mRealPlayFullSoundBtn != null) {
				mRealPlayFullSoundBtn
						.setBackgroundResource(R.drawable.play_full_soundoff_btn_selector);
			}
		} else {
			mLocalInfo.setSoundOpen(true);
			mRealPlaySoundBtn
					.setBackgroundResource(R.drawable.ezopen_vertical_preview_sound_selector);
			if (mRealPlayFullSoundBtn != null) {
				mRealPlayFullSoundBtn
						.setBackgroundResource(R.drawable.play_full_soundon_btn_selector);
			}
		}

		setRealPlaySound();
	}

	private void setRealPlaySound() {
		if (mEZPlayer != null) {
			if (mRtspUrl == null) {
				if (mLocalInfo.isSoundOpen()) {
					mEZPlayer.openSound();
				} else {
					mEZPlayer.closeSound();
				}
			} else {
				if (mRealPlaySquareInfo.mSoundType == 0) {
					mEZPlayer.closeSound();
				} else {
					mEZPlayer.openSound();
				}
			}
		}
	}

	private OnClickListener mOnPopWndClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.quality_hd_btn:
				setQualityMode(EZVideoLevel.VIDEO_LEVEL_HD);
				break;
			case R.id.quality_balanced_btn:
				setQualityMode(EZVideoLevel.VIDEO_LEVEL_BALANCED);
				break;
			case R.id.quality_flunet_btn:
				setQualityMode(EZVideoLevel.VIDEO_LEVEL_FLUNET);
				break;
			default:
				break;
			}
		}
	};

	private String verifyCode;

	public void setSoundLocalization(int status) {
	}

	/**
	 * 码流配置 清晰度 2-高清，1-标清，0-流畅
	 * 
	 * @see
	 * @since V2.0
	 */
	private void setQualityMode(final EZVideoLevel mode) {
		// 检查网络是否可用
		if (!ConnectionDetector.isNetworkAvailable(VideoPlayerActivity.this)) {
			// 提示没有连接网络
			Utils.showToast(VideoPlayerActivity.this,
					R.string.realplay_set_fail_network);
			return;
		}

		if (mEZPlayer != null) {
			mWaitDialog.setWaitText(this
					.getString(R.string.setting_video_level));
			mWaitDialog.show();

			Thread thr = new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						// need to modify by yudan at 08-11
						App.getOpenSDK()
								.setVideoLevel(mCameraInfo.getDeviceSerial(),
										mCameraInfo.getCameraNo(),
										mode.getVideoLevel());
						mCurrentQulityMode = mode;
						Message msg = Message.obtain();
						msg.what = MSG_SET_VEDIOMODE_SUCCESS;
						mHandler.sendMessage(msg);
						LogUtil.i(TAG, "setQualityMode success");
					} catch (BaseException e) {
						mCurrentQulityMode = EZVideoLevel.VIDEO_LEVEL_FLUNET;
						e.printStackTrace();
						Message msg = Message.obtain();
						msg.what = MSG_SET_VEDIOMODE_FAIL;
						mHandler.sendMessage(msg);
						LogUtil.i(TAG, "setQualityMode fail");
					}

				}
			}) {
			};
			thr.start();
		}
	}

	private void openQualityPopupWindow(View anchor) {
		if (mEZPlayer == null) {
			return;
		}
		closeQualityPopupWindow();
		LayoutInflater layoutInflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewGroup layoutView = (ViewGroup) layoutInflater.inflate(
				R.layout.realplay_quality_items, null, true);

		Button qualityHdBtn = (Button) layoutView
				.findViewById(R.id.quality_hd_btn);
		qualityHdBtn.setOnClickListener(mOnPopWndClickListener);
		Button qualityBalancedBtn = (Button) layoutView
				.findViewById(R.id.quality_balanced_btn);
		qualityBalancedBtn.setOnClickListener(mOnPopWndClickListener);
		Button qualityFlunetBtn = (Button) layoutView
				.findViewById(R.id.quality_flunet_btn);
		qualityFlunetBtn.setOnClickListener(mOnPopWndClickListener);

		// 视频质量，2-高清，1-标清，0-流畅
		if (mCameraInfo.getVideoLevel() == EZVideoLevel.VIDEO_LEVEL_FLUNET) {
			qualityFlunetBtn.setEnabled(false);
		} else if (mCameraInfo.getVideoLevel() == EZVideoLevel.VIDEO_LEVEL_BALANCED) {
			qualityBalancedBtn.setEnabled(false);
		} else if (mCameraInfo.getVideoLevel() == EZVideoLevel.VIDEO_LEVEL_HD) {
			qualityHdBtn.setEnabled(false);
		}

		int height = 105;

		qualityFlunetBtn.setVisibility(View.VISIBLE);
		qualityBalancedBtn.setVisibility(View.VISIBLE);
		qualityHdBtn.setVisibility(View.VISIBLE);

		height = Utils.dip2px(this, height);
		mQualityPopupWindow = new PopupWindow(layoutView,
				LayoutParams.WRAP_CONTENT, height, true);
		mQualityPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mQualityPopupWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				LogUtil.infoLog(TAG, "KEYCODE_BACK DOWN");
				mQualityPopupWindow = null;
				closeQualityPopupWindow();
			}
		});
		try {
			mQualityPopupWindow.showAsDropDown(anchor, -Utils.dip2px(this, 5),
					-(height + anchor.getHeight() + Utils.dip2px(this, 8)));
		} catch (Exception e) {
			e.printStackTrace();
			closeQualityPopupWindow();
		}
	}

	private void closeQualityPopupWindow() {
		if (mQualityPopupWindow != null) {
			dismissPopWindow(mQualityPopupWindow);
			mQualityPopupWindow = null;
		}
	}

	/**
	 * 开始录像 录制到手机本地 本地录像
	 * 
	 * @see
	 * @since V1.0
	 */
	private void onRecordBtnClick() {
		mControlDisplaySec = 0;
		if (mIsRecording) {
			stopRealPlayRecord();
			return;
		}
		if (!SDCardUtil.isSDCardUseable()) {
			// 提示SD卡不可用
			Utils.showToast(VideoPlayerActivity.this,
					R.string.remoteplayback_SDCard_disable_use);
			return;
		}
		if (SDCardUtil.getSDCardRemainSize() < SDCardUtil.PIC_MIN_MEM_SPACE) {
			// 提示内存不足
			Utils.showToast(VideoPlayerActivity.this,
					R.string.remoteplayback_record_fail_for_memory);
			return;
		}
		if (mEZPlayer != null) {
			mCaptureDisplaySec = 4;
			updateCaptureUI();
			mAudioPlayUtil.playAudioFile(AudioPlayUtil.RECORD_SOUND);
			// 可以采用deviceSerial+时间作为文件命名，demo中简化，只用时间命名
			java.util.Date date = new java.util.Date();
			String strRecordFile = Environment.getExternalStorageDirectory()
					.getPath()
					+ "/EZOpenSDK/Records/"
					+ String.format("%tY", date)
					+ String.format("%tm", date)
					+ String.format("%td", date)
					+ "/"
					+ String.format("%tH", date)
					+ String.format("%tM", date)
					+ String.format("%tS", date)
					+ String.format("%tL", date)
					+ ".mp4";
			if (mEZPlayer.startLocalRecordWithFile(strRecordFile)) {
				handleRecordSuccess(strRecordFile);
			} else {
				handleRecordFail();
			}
		}
	}

	/**
	 * 停止录像
	 * 
	 * @see
	 * @since V1.0
	 */
	private void stopRealPlayRecord() {
		if (mEZPlayer == null || !mIsRecording) {
			return;
		}
		Toast.makeText(VideoPlayerActivity.this,
				getResources().getString(R.string.already_saved_to_volume),
				Toast.LENGTH_SHORT).show();

		// 设置录像按钮为check状态
		if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
			if (!mIsOnStop) {
				mRecordRotateViewUtil.applyRotation(mRealPlayRecordContainer,
						mRealPlayRecordStartBtn, mRealPlayRecordBtn, 0, 90);
			} else {
				mRealPlayRecordStartBtn.setVisibility(View.GONE);
				mRealPlayRecordBtn.setVisibility(View.VISIBLE);
			}
			mRealPlayFullRecordStartBtn.setVisibility(View.GONE);
			mRealPlayFullRecordBtn.setVisibility(View.VISIBLE);
		} else {
			if (!mIsOnStop) {
				mRecordRotateViewUtil.applyRotation(
						mRealPlayFullRecordContainer,
						mRealPlayFullRecordStartBtn, mRealPlayFullRecordBtn, 0,
						90);
			} else {
				mRealPlayFullRecordStartBtn.setVisibility(View.GONE);
				mRealPlayFullRecordBtn.setVisibility(View.VISIBLE);

			}
			mRealPlayRecordStartBtn.setVisibility(View.GONE);
			mRealPlayRecordBtn.setVisibility(View.VISIBLE);
		}
		mAudioPlayUtil.playAudioFile(AudioPlayUtil.RECORD_SOUND);
		mEZPlayer.stopLocalRecord();

		// 计时按钮不可见
		mRealPlayRecordLy.setVisibility(View.GONE);
		// mRealPlayCaptureRl.setVisibility(View.VISIBLE);
		mCaptureDisplaySec = 0;
		// try {
		// mRealPlayCaptureIv.setImageURI(Uri.parse(mRecordFilePath));
		// } catch (OutOfMemoryError e) {
		// e.printStackTrace();
		// }
		// mRealPlayCaptureWatermarkIv.setTag(mRecordFilePath);
		mIsRecording = false;
		updateCaptureUI();
	}

	private void onCaptureRlClick() {
	}

	/**
	 * 抓拍按钮响应函数
	 * 
	 * @since V1.0
	 */
	private void onCapturePicBtnClick() {

		mControlDisplaySec = 0;
		if (!SDCardUtil.isSDCardUseable()) {
			// 提示SD卡不可用
			Utils.showToast(VideoPlayerActivity.this,
					R.string.remoteplayback_SDCard_disable_use);
			return;
		}

		if (SDCardUtil.getSDCardRemainSize() < SDCardUtil.PIC_MIN_MEM_SPACE) {
			// 提示内存不足
			Utils.showToast(VideoPlayerActivity.this,
					R.string.remoteplayback_capture_fail_for_memory);
			return;
		}

		if (mEZPlayer != null) {
			mCaptureDisplaySec = 4;
			updateCaptureUI();

			Thread thr = new Thread() {
				@Override
				public void run() {
					Bitmap bmp = mEZPlayer.capturePicture();
					if (bmp != null) {
						try {
							mAudioPlayUtil
									.playAudioFile(AudioPlayUtil.CAPTURE_SOUND);

							// 可以采用deviceSerial+时间作为文件命名，demo中简化，只用时间命名
							java.util.Date date = new java.util.Date();
							final String path = Environment
									.getExternalStorageDirectory().getPath()
									+ "/EZOpenSDK/CapturePicture/"
									+ String.format("%tY", date)
									+ String.format("%tm", date)
									+ String.format("%td", date)
									+ "/"
									+ String.format("%tH", date)
									+ String.format("%tM", date)
									+ String.format("%tS", date)
									+ String.format("%tL", date) + ".jpg";

							if (TextUtils.isEmpty(path)) {
								bmp.recycle();
								bmp = null;
								return;
							}
							EZUtils.saveCapturePictrue(path, bmp);

							MediaScanner mMediaScanner = new MediaScanner(
									VideoPlayerActivity.this);
							mMediaScanner.scanFile(path, "jpg");
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(
											VideoPlayerActivity.this,
											getResources()
													.getString(
															R.string.already_saved_to_volume)
													+ path, Toast.LENGTH_SHORT)
											.show();
								}
							});
						} catch (InnerException e) {
							e.printStackTrace();
						} finally {
							if (bmp != null) {
								bmp.recycle();
								bmp = null;
								return;
							}
						}
					}
					super.run();
				}
			};
			thr.start();
		}
	}

	private void onRealPlaySvClick() {
		if (mCameraInfo != null && mEZPlayer != null && mDeviceInfo != null) {
			if (mDeviceInfo.getStatus() != 1) {
				return;
			}
			if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
				setRealPlayControlRlVisibility();
			} else {
				setRealPlayFullOperateBarVisibility();
			}
		} else if (mRtspUrl != null) {
			setRealPlayControlRlVisibility();
		}
	}

	private void setRealPlayControlRlVisibility() {
		if (mLandscapeTitleBar.getVisibility() == View.VISIBLE
				|| mRealPlayControlRl.getVisibility() == View.VISIBLE) {
			// mRealPlayControlRl.setVisibility(View.GONE);
			mLandscapeTitleBar.setVisibility(View.GONE);
			closeQualityPopupWindow();
		} else {
			mRealPlayControlRl.setVisibility(View.VISIBLE);
			if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
				mLandscapeTitleBar.setVisibility(View.VISIBLE);
			} else {
				mLandscapeTitleBar.setVisibility(View.GONE);
			}
			mControlDisplaySec = 0;
		}
	}

	private void setRealPlayFullOperateBarVisibility() {
		if (mLandscapeTitleBar.getVisibility() == View.VISIBLE) {
			mRealPlayFullOperateBar.setVisibility(View.GONE);
			mFullscreenFullButton.setVisibility(View.GONE);
			mLandscapeTitleBar.setVisibility(View.GONE);
		} else {
			mLandscapeTitleBar.setVisibility(View.VISIBLE);
			mControlDisplaySec = 0;
		}
	}

	/**
	 * 开始播放
	 * 
	 * @see
	 * @since V2.0
	 */
	private void startRealPlay() {
		// 增加手机客户端操作信息记录
		LogUtil.debugLog(TAG, "startRealPlay");

		if (mStatus == RealPlayStatus.STATUS_START
				|| mStatus == RealPlayStatus.STATUS_PLAY) {
			return;
		}

		// 检查网络是否可用
		if (!ConnectionDetector.isNetworkAvailable(this)) {
			// 提示没有连接网络
			setRealPlayFailUI(getString(R.string.realplay_play_fail_becauseof_network));
			return;
		}

		mStatus = RealPlayStatus.STATUS_START;
		setRealPlayLoadingUI();

		if (mCameraInfo != null) {
			if (mEZPlayer == null) {
				mEZPlayer = App.getOpenSDK().createPlayer(
						mCameraInfo.getDeviceSerial(),
						mCameraInfo.getCameraNo());
			}

			if (mEZPlayer == null)
				return;
			if (mDeviceInfo == null) {
				return;
			}
			if (mDeviceInfo.getIsEncrypt() == 1) {
				mEZPlayer.setPlayVerifyCode(DataManager.getInstance()
						.getDeviceSerialVerifyCode(
								mCameraInfo.getDeviceSerial()));
			}

			mEZPlayer.setHandler(mHandler);
			mEZPlayer.setSurfaceHold(mRealPlaySh);
			mEZPlayer.startRealPlay();
		} else if (mRtspUrl != null) {
			mEZPlayer = App.getOpenSDK().createPlayerWithUrl(
					mRtspUrl);
			// mStub.setCameraId(mCameraInfo.getCameraId());////**** mj
			if (mEZPlayer == null)
				return;
			mEZPlayer.setHandler(mHandler);
			mEZPlayer.setSurfaceHold(mRealPlaySh);

			mEZPlayer.startRealPlay();
		}
		updateLoadingProgress(0);
	}

	/**
	 * 停止播放
	 * 
	 * @see
	 * @since V1.0
	 */
	private void stopRealPlay() {
		LogUtil.debugLog(TAG, "stopRealPlay");
		mStatus = RealPlayStatus.STATUS_STOP;

		stopUpdateTimer();
		if (mEZPlayer != null) {
			stopRealPlayRecord();

			mEZPlayer.stopRealPlay();
		}
		mStreamFlow = 0;
	}

	private void setRealPlayLoadingUI() {
		mStartTime = System.currentTimeMillis();
		mRealPlaySv.setVisibility(View.INVISIBLE);
		mRealPlaySv.setVisibility(View.VISIBLE);
		setStartloading();
		mRealPlayBtn.setBackgroundResource(R.drawable.play_stop_selector);

		if (mCameraInfo != null && mDeviceInfo != null) {
			mRealPlayCaptureBtn.setEnabled(false);
			mRealPlayRecordBtn.setEnabled(false);
			if (mDeviceInfo.getStatus() == 1) {
				mRealPlayQualityBtn.setEnabled(true);
			} else {
				mRealPlayQualityBtn.setEnabled(false);
			}

			mRealPlayFullPlayBtn
					.setBackgroundResource(R.drawable.play_full_stop_selector);
			mRealPlayFullCaptureBtn.setEnabled(false);
			mRealPlayFullRecordBtn.setEnabled(false);
			mRealPlayFullFlowLy.setVisibility(View.GONE);
		}

		showControlRlAndFullOperateBar();
	}

	private void showControlRlAndFullOperateBar() {
		if (mRtspUrl != null
				|| mOrientation == Configuration.ORIENTATION_PORTRAIT) {
			mRealPlayControlRl.setVisibility(View.VISIBLE);
			if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
				mLandscapeTitleBar.setVisibility(View.VISIBLE);
			} else {
				mLandscapeTitleBar.setVisibility(View.GONE);
			}
			mControlDisplaySec = 0;
		} else {
			mRealPlayFullOperateBar.setVisibility(View.VISIBLE);
			// mFullscreenFullButton.setVisibility(View.VISIBLE);
			mLandscapeTitleBar.setVisibility(View.VISIBLE);
			mControlDisplaySec = 0;
		}
	}

	private void setRealPlayStopUI() {
		stopUpdateTimer();
		updateOrientation();
		setRealPlaySvLayout();
		setStopLoading();
		hideControlRlAndFullOperateBar(true);
		mRealPlayBtn.setBackgroundResource(R.drawable.play_play_selector);
		if (mCameraInfo != null && mDeviceInfo != null) {
			mRealPlayCaptureBtn.setEnabled(false);
			mRealPlayRecordBtn.setEnabled(false);
			if (mDeviceInfo.getStatus() == 1) {
				mRealPlayQualityBtn.setEnabled(true);
			} else {
				mRealPlayQualityBtn.setEnabled(false);
			}
			if (mDeviceInfo.getStatus() == 1) {
				mRealPlayPrivacyBtn.setEnabled(true);
				mRealPlaySslBtn.setEnabled(true);
			} else {
				mRealPlayPrivacyBtn.setEnabled(false);
				mRealPlaySslBtn.setEnabled(false);
			}

			mRealPlayFullPlayBtn
					.setBackgroundResource(R.drawable.play_full_play_selector);
			mRealPlayFullCaptureBtn.setEnabled(false);
			mRealPlayFullRecordBtn.setEnabled(false);
		}
	}

	private void setRealPlayFailUI(String errorStr) {
		mStopTime = System.currentTimeMillis();
		showType();

		stopUpdateTimer();
		updateOrientation();

		{
			setLoadingFail(errorStr);
		}
		mRealPlayFullFlowLy.setVisibility(View.GONE);
		mRealPlayBtn.setBackgroundResource(R.drawable.play_play_selector);

		hideControlRlAndFullOperateBar(true);

		if (mCameraInfo != null && mDeviceInfo != null) {
			mRealPlayCaptureBtn.setEnabled(false);
			mRealPlayRecordBtn.setEnabled(false);
			if (mDeviceInfo.getStatus() == 1 && (mEZPlayer == null)) {
				mRealPlayQualityBtn.setEnabled(true);
			} else {
				mRealPlayQualityBtn.setEnabled(false);
			}
			if (mDeviceInfo.getStatus() == 1) {
				mRealPlayPrivacyBtn.setEnabled(true);
				mRealPlaySslBtn.setEnabled(true);
			} else {
				mRealPlayPrivacyBtn.setEnabled(false);
				mRealPlaySslBtn.setEnabled(false);
			}

			mRealPlayFullPlayBtn
					.setBackgroundResource(R.drawable.play_full_play_selector);
			mRealPlayFullCaptureBtn.setEnabled(false);
			mRealPlayFullRecordBtn.setEnabled(false);
		}
	}

	private void setRealPlaySuccessUI() {
		mStopTime = System.currentTimeMillis();
		showType();

		updateOrientation();
		setLoadingSuccess();
		mRealPlayFlowTv.setVisibility(View.VISIBLE);
		mRealPlayFullFlowLy.setVisibility(View.VISIBLE);
		mRealPlayBtn.setBackgroundResource(R.drawable.play_stop_selector);

		if (mCameraInfo != null && mDeviceInfo != null) {
			mRealPlayCaptureBtn.setEnabled(true);
			mRealPlayRecordBtn.setEnabled(true);
			if (mDeviceInfo.getStatus() == 1) {
				mRealPlayQualityBtn.setEnabled(true);
			} else {
				mRealPlayQualityBtn.setEnabled(false);
			}
			if (getSupportPtz() == 1) {
			}

			mRealPlayFullPlayBtn
					.setBackgroundResource(R.drawable.play_full_stop_selector);
			mRealPlayFullCaptureBtn.setEnabled(true);
			mRealPlayFullRecordBtn.setEnabled(true);
		}

		// setRealPlaySound();

		startUpdateTimer();
	}

	/**
	 * 更新流量统计
	 * 
	 * @see
	 * @since V1.0
	 */
	private void checkRealPlayFlow() {
		if ((mEZPlayer != null && mRealPlayFlowTv.getVisibility() == View.VISIBLE)) {
			// 更新流量数据
			long streamFlow = mEZPlayer.getStreamFlow();
			updateRealPlayFlowTv(streamFlow);
		}
	}

	private void updateRealPlayFlowTv(long streamFlow) {
		long streamFlowUnit = streamFlow - mStreamFlow;
		if (streamFlowUnit < 0)
			streamFlowUnit = 0;
		float fKBUnit = (float) streamFlowUnit / (float) Constant.KB;
		String descUnit = String.format("%.2f k/s ", fKBUnit);
		// 显示流量
		mRealPlayFlowTv.setText(descUnit);
		mStreamFlow = streamFlow;
	}

	private void setOrientation(int sensor) {
		if (mForceOrientation != 0) {
			LogUtil.debugLog(TAG, "setOrientation mForceOrientation:"
					+ mForceOrientation);
			return;
		}

		if (sensor == ActivityInfo.SCREEN_ORIENTATION_SENSOR)
			mScreenOrientationHelper.enableSensorOrientation();
		else
			mScreenOrientationHelper.disableSensorOrientation();
	}

	public void setForceOrientation(int orientation) {
		if (mForceOrientation == orientation) {
			LogUtil.debugLog(TAG, "setForceOrientation no change");
			return;
		}
		mForceOrientation = orientation;
		if (mForceOrientation != 0) {
			if (mForceOrientation != mOrientation) {
				if (mForceOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
					mScreenOrientationHelper.portrait();
				} else {
					mScreenOrientationHelper.landscape();
				}
			}
			mScreenOrientationHelper.disableSensorOrientation();
		} else {
			updateOrientation();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.Handler.Callback#handleMessage(android.os.Message)
	 */
	@SuppressLint("NewApi")
	@Override
	public boolean handleMessage(Message msg) {
		// LogUtil.infoLog(TAG, "handleMessage:" + msg.what);
		if (this.isFinishing()) {
			return false;
		}
		switch (msg.what) {
		case EZRealPlayConstants.MSG_GET_CAMERA_INFO_SUCCESS:
			updateLoadingProgress(20);
			handleGetCameraInfoSuccess();
			break;
		case EZRealPlayConstants.MSG_REALPLAY_PLAY_START:
			updateLoadingProgress(40);
			break;
		case EZRealPlayConstants.MSG_REALPLAY_CONNECTION_START:
			updateLoadingProgress(60);
			break;
		case EZRealPlayConstants.MSG_REALPLAY_CONNECTION_SUCCESS:
			updateLoadingProgress(80);
			break;
		case EZRealPlayConstants.MSG_REALPLAY_PLAY_SUCCESS:
			handlePlaySuccess(msg);
			break;
		case EZRealPlayConstants.MSG_REALPLAY_PLAY_FAIL:
			handlePlayFail(msg.obj);
			break;
		case EZRealPlayConstants.MSG_SET_VEDIOMODE_SUCCESS:
			handleSetVedioModeSuccess();
			break;
		case EZRealPlayConstants.MSG_SET_VEDIOMODE_FAIL:
			handleSetVedioModeFail(msg.arg1);
			break;
		case EZRealPlayConstants.MSG_PTZ_SET_FAIL:
			break;
		case EZRealPlayConstants.MSG_REALPLAY_VOICETALK_SUCCESS:
			break;
		case EZRealPlayConstants.MSG_REALPLAY_VOICETALK_STOP:
			break;
		case EZRealPlayConstants.MSG_REALPLAY_VOICETALK_FAIL:
			ErrorInfo errorInfo = (ErrorInfo) msg.obj;
			break;
		case MSG_PLAY_UI_UPDATE:
			updateRealPlayUI();
			break;
		case MSG_AUTO_START_PLAY:
			startRealPlay();
			break;
		case MSG_CLOSE_PTZ_PROMPT:
			break;
		case MSG_HIDE_PTZ_DIRECTION:
			break;
		case MSG_HIDE_PAGE_ANIM:
			hidePageAnim();
			break;
		case MSG_PLAY_UI_REFRESH:
			initUI();
			break;
		case MSG_PREVIEW_START_PLAY:
			mPageAnimIv.setVisibility(View.GONE);
			mRealPlayPreviewTv.setVisibility(View.GONE);
			mStatus = RealPlayStatus.STATUS_INIT;
			startRealPlay();
			break;
		default:
			break;
		}
		return false;
	}

	private void hidePageAnim() {
		mHandler.removeMessages(MSG_HIDE_PAGE_ANIM);
		if (mPageAnimDrawable != null) {
			if (mPageAnimDrawable.isRunning()) {
				mPageAnimDrawable.stop();
			}
			mPageAnimDrawable = null;
			mPageAnimIv.setBackgroundDrawable(null);
			mPageAnimIv.setVisibility(View.GONE);
		}
		if (mPageAnimIv != null) {
			mPageAnimIv.setBackgroundDrawable(null);
			mPageAnimIv.setVisibility(View.GONE);
		}
	}

	private void updateUI() {
		// 通过能力级设置
		setVideoLevel();
		/*
		 * if (mRealPlayMgr != null && mRealPlayMgr.getSupportPtzPrivacy() == 1)
		 * { mRealPlayPrivacyBtnLy.setVisibility(View.VISIBLE); if
		 * (mCameraInfo.getOnlineStatus() == 1 &&
		 * mRealPlayMgr.getPrivacyStatus() != 1) {
		 * mRealPlayQualityBtn.setEnabled(true); } else {
		 * mRealPlayQualityBtn.setEnabled(false); } } else {
		 * mRealPlayPrivacyBtnLy.setVisibility(View.GONE); }
		 */
		// mRealPlayPrivacyBtnLy.setVisibility(View.GONE);
		// setSoundLocalizationUI();

		{
			mRealPlaySslBtnLy.setVisibility(View.GONE);
		}

		// updatePermissionUI();
	}

	/**
	 * 获取设备信息成功
	 * 
	 * @see
	 * @since V1.0
	 */
	private void handleGetCameraInfoSuccess() {
		LogUtil.infoLog(TAG, "handleGetCameraInfoSuccess");

		// 通过能力级设置
		updateUI();

	}

	private void handleSetVedioModeSuccess() {
		closeQualityPopupWindow();
		setVideoLevel();
		try {
			mWaitDialog.setWaitText(null);
			mWaitDialog.dismiss();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (mStatus == RealPlayStatus.STATUS_PLAY) {
			// 停止播放
			stopRealPlay();
			// 下面语句防止stopRealPlay线程还没释放surface, startRealPlay线程已经开始使用surface
			// 因此需要等待500ms
			SystemClock.sleep(500);
			// 开始播放
			startRealPlay();
		}
	}

	private void handleSetVedioModeFail(int errorCode) {
		closeQualityPopupWindow();
		setVideoLevel();
		try {
			mWaitDialog.setWaitText(null);
			mWaitDialog.dismiss();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Utils.showToast(VideoPlayerActivity.this,
				R.string.realplay_set_vediomode_fail, errorCode);
	}

	/**
	 * 开始录像成功
	 * 
	 * @param recordFilePath
	 * @see
	 * @since V2.0
	 */
	private void handleRecordSuccess(String recordFilePath) {
		if (mCameraInfo == null) {
			return;
		}

		// 设置录像按钮为check状态
		if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
			if (!mIsOnStop) {
				mRecordRotateViewUtil.applyRotation(mRealPlayRecordContainer,
						mRealPlayRecordBtn, mRealPlayRecordStartBtn, 0, 90);
			} else {
				mRealPlayRecordBtn.setVisibility(View.GONE);
				mRealPlayRecordStartBtn.setVisibility(View.VISIBLE);
			}
			mRealPlayFullRecordBtn.setVisibility(View.GONE);
			mRealPlayFullRecordStartBtn.setVisibility(View.VISIBLE);
		} else {
			if (!mIsOnStop) {
				mRecordRotateViewUtil.applyRotation(
						mRealPlayFullRecordContainer, mRealPlayFullRecordBtn,
						mRealPlayFullRecordStartBtn, 0, 90);
			} else {
				mRealPlayFullRecordBtn.setVisibility(View.GONE);
				mRealPlayFullRecordStartBtn.setVisibility(View.VISIBLE);
			}
			mRealPlayRecordBtn.setVisibility(View.GONE);
			mRealPlayRecordStartBtn.setVisibility(View.VISIBLE);
		}
		mIsRecording = true;
		// 计时按钮可见
		mRealPlayRecordLy.setVisibility(View.VISIBLE);
		mRealPlayRecordTv.setText("00:00");
		mRecordSecond = 0;
	}

	private void handleRecordFail() {
		Utils.showToast(VideoPlayerActivity.this,
				R.string.remoteplayback_record_fail);
		if (mIsRecording) {
			stopRealPlayRecord();
		}
	}

	private void hideControlRlAndFullOperateBar(boolean excludeLandscapeTitle) {
		// mRealPlayControlRl.setVisibility(View.GONE);
		closeQualityPopupWindow();
		if (mRealPlayFullOperateBar != null) {
			mRealPlayFullOperateBar.setVisibility(View.GONE);
			if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
				mFullscreenFullButton.setVisibility(View.GONE);
			} else {
				mFullscreenFullButton.setVisibility(View.GONE);
			}
		}
		if (excludeLandscapeTitle
				&& mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
			mLandscapeTitleBar.setVisibility(View.VISIBLE);
		} else {
			mLandscapeTitleBar.setVisibility(View.GONE);
		}
	}

	private void updateRealPlayUI() {
		if (mControlDisplaySec == 5) {
			mControlDisplaySec = 0;
			hideControlRlAndFullOperateBar(false);
		}
		checkRealPlayFlow();
		updateCaptureUI();

		if (mIsRecording) {
			updateRecordTime();
		}
	}

	private void initCaptureUI() {
		mCaptureDisplaySec = 0;
		mRealPlayCaptureRl.setVisibility(View.GONE);
		mRealPlayCaptureIv.setImageURI(null);
		mRealPlayCaptureWatermarkIv.setTag(null);
		mRealPlayCaptureWatermarkIv.setVisibility(View.GONE);
	}

	// 更新抓图/录像显示UI
	private void updateCaptureUI() {
		if (mRealPlayCaptureRl.getVisibility() == View.VISIBLE) {
			if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
				if (mRealPlayControlRl.getVisibility() == View.VISIBLE) {
					mRealPlayCaptureRlLp.setMargins(0, 0, 0,
							Utils.dip2px(this, 40));
				} else {
					mRealPlayCaptureRlLp.setMargins(0, 0, 0, 0);
				}
				mRealPlayCaptureRl.setLayoutParams(mRealPlayCaptureRlLp);
			} else {
				LayoutParams realPlayCaptureRlLp = new LayoutParams(
						Utils.dip2px(this, 65), Utils.dip2px(this, 45));
				realPlayCaptureRlLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
				realPlayCaptureRlLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				mRealPlayCaptureRl.setLayoutParams(realPlayCaptureRlLp);
			}
			if (mRealPlayCaptureWatermarkIv.getTag() != null) {
				mRealPlayCaptureWatermarkIv.setVisibility(View.VISIBLE);
				mRealPlayCaptureWatermarkIv.setTag(null);
			}
		}
		if (mCaptureDisplaySec >= 4) {
			initCaptureUI();
		}
	}

	/**
	 * 更新录像时间
	 * 
	 * @see
	 * @since V1.0
	 */
	private void updateRecordTime() {
		if (mRealPlayRecordIv.getVisibility() == View.VISIBLE) {
			mRealPlayRecordIv.setVisibility(View.INVISIBLE);
		} else {
			mRealPlayRecordIv.setVisibility(View.VISIBLE);
		}
		// 计算分秒
		int leftSecond = mRecordSecond % 3600;
		int minitue = leftSecond / 60;
		int second = leftSecond % 60;

		// 显示录像时间
		String recordTime = String.format("%02d:%02d", minitue, second);
		mRealPlayRecordTv.setText(recordTime);
	}

	// 处理密码错误
	private void handlePasswordError(int title_resid, int msg1_resid,
			int msg2_resid) {
		stopRealPlay();
		setRealPlayStopUI();
		LogUtil.debugLog(TAG, "startRealPlay");

		if (mCameraInfo == null || mStatus == RealPlayStatus.STATUS_START
				|| mStatus == RealPlayStatus.STATUS_PLAY) {
			return;
		}

		// 检查网络是否可用
		if (!ConnectionDetector.isNetworkAvailable(this)) {
			// 提示没有连接网络
			setRealPlayFailUI(getString(R.string.realplay_play_fail_becauseof_network));
			return;
		}

		mStatus = RealPlayStatus.STATUS_START;
		setRealPlayLoadingUI();

		updateLoadingProgress(0);
	}

	/**
	 * 处理播放成功的情况
	 * 
	 * @see
	 * @since V1.0
	 */
	private void handlePlaySuccess(Message msg) {
		LogUtil.d(TAG, "handlePlaySuccess");
		mStatus = RealPlayStatus.STATUS_PLAY;

		// 声音处理
		setRealPlaySound();

		// temp solution for OPENSDK-92
		// Android 预览3Q10的时候切到流畅之后 视频播放窗口变大了
		// if (msg.arg1 != 0) {
		// mRealRatio = (float) msg.arg2 / msg.arg1;
		// } else {
		// mRealRatio = Constant.LIVE_VIEW_RATIO;
		// }
		mRealRatio = Constant.LIVE_VIEW_RATIO;

		boolean bSupport = true;// (float) mLocalInfo.getScreenHeight() /
								// mLocalInfo.getScreenWidth() >=
								// BIG_SCREEN_RATIO;
		if (bSupport) {
			initOperateBarUI();
			initUI();
			if (mRealRatio <= Constant.LIVE_VIEW_RATIO) {
				setBigScreenOperateBtnLayout();
			}
		}
		setRealPlaySvLayout();
		setRealPlaySuccessUI();
		if (mEZPlayer != null) {
			mStreamFlow = mEZPlayer.getStreamFlow();
		}
	}

	private void setRealPlaySvLayout() {
		// 设置播放窗口位置
		final int screenWidth = mLocalInfo.getScreenWidth();
		final int screenHeight = (mOrientation == Configuration.ORIENTATION_PORTRAIT) ? (mLocalInfo
				.getScreenHeight() - mLocalInfo.getNavigationBarHeight())
				: mLocalInfo.getScreenHeight();
		final LayoutParams realPlaySvlp = Utils.getPlayViewLp(mRealRatio,
				mOrientation, mLocalInfo.getScreenWidth(),
				(int) (mLocalInfo.getScreenWidth() * Constant.LIVE_VIEW_RATIO),
				screenWidth, screenHeight);

		LayoutParams loadingR1Lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				realPlaySvlp.height);
		// loadingR1Lp.addRule(RelativeLayout.CENTER_IN_PARENT);
		// mRealPlayLoadingRl.setLayoutParams(loadingR1Lp);
		// mRealPlayPromptRl.setLayoutParams(loadingR1Lp);
		LayoutParams svLp = new LayoutParams(realPlaySvlp.width,
				realPlaySvlp.height);
		// mj svLp.addRule(RelativeLayout.CENTER_IN_PARENT);
		mRealPlaySv.setLayoutParams(svLp);

		if (mRtspUrl == null) {
			// LinearLayout.LayoutParams realPlayPlayRlLp = new
			// LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
			// LayoutParams.WRAP_CONTENT);
			// realPlayPlayRlLp.gravity = Gravity.CENTER;
			// mRealPlayPlayRl.setLayoutParams(realPlayPlayRlLp);
		} else {
			LinearLayout.LayoutParams realPlayPlayRlLp = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			realPlayPlayRlLp.gravity = Gravity.CENTER;
			// realPlayPlayRlLp.weight = 1;
			mRealPlayPlayRl.setLayoutParams(realPlayPlayRlLp);
		}
		mRealPlayTouchListener.setSacaleRect(Constant.MAX_SCALE, 0, 0,
				realPlaySvlp.width, realPlaySvlp.height);
		setPlayScaleUI(1, null, null);
	}

	/**
	 * 处理播放失败的情况
	 * 
	 * @param obj
	 *            - 错误码
	 * @see
	 * @since V1.0
	 */
	private void handlePlayFail(Object obj) {
		int errorCode = 0;
		if (obj != null) {
			ErrorInfo errorInfo = (ErrorInfo) obj;
			errorCode = errorInfo.errorCode;
			LogUtil.debugLog(TAG, "handlePlayFail:" + errorInfo.errorCode);
		}

		hidePageAnim();

		stopRealPlay();

		updateRealPlayFailUI(errorCode);
	}

	private void updateRealPlayFailUI(int errorCode) {
		String txt = null;
		LogUtil.e(TAG, "updateRealPlayFailUI: errorCode:" + errorCode);
		// 判断返回的错误码
		switch (errorCode) {
		case ErrorCode.ERROR_TRANSF_ACCESSTOKEN_ERROR:
			return;
		case ErrorCode.ERROR_CAS_MSG_PU_NO_RESOURCE:
			txt = getString(R.string.remoteplayback_over_link);
			break;
		case ErrorCode.ERROR_TRANSF_DEVICE_OFFLINE:
			if (mCameraInfo != null) {
				mCameraInfo.setIsShared(0);
			}
			txt = getString(R.string.realplay_fail_device_not_exist);
			break;
		case ErrorCode.ERROR_INNER_STREAM_TIMEOUT:
			txt = getString(R.string.realplay_fail_connect_device);
			break;
		case ErrorCode.ERROR_WEB_CODE_ERROR:
			// VerifySmsCodeUtil.openSmsVerifyDialog(Constant.SMS_VERIFY_LOGIN,
			// this, this);
			// txt = Utils.getErrorTip(this, R.string.check_feature_code_fail,
			// errorCode);
			break;
		case ErrorCode.ERROR_WEB_HARDWARE_SIGNATURE_OP_ERROR:
			// VerifySmsCodeUtil.openSmsVerifyDialog(Constant.SMS_VERIFY_HARDWARE,
			// this, null);
			// SecureValidate.secureValidateDialog(this, this);
			// txt = Utils.getErrorTip(this, R.string.check_feature_code_fail,
			// errorCode);
			break;
		case ErrorCode.ERROR_TRANSF_TERMINAL_BINDING:
			txt = "请在萤石客户端关闭终端绑定";
			break;
		// 收到这两个错误码，可以弹出对话框，让用户输入密码后，重新取流预览
		case ErrorCode.ERROR_INNER_VERIFYCODE_NEED:
		case ErrorCode.ERROR_INNER_VERIFYCODE_ERROR:
			// DataManager.getInstance().setDeviceSerialVerifyCode(mCameraInfo.getDeviceSerial(),
			// null);
			// VerifyCodeInput.VerifyCodeInputDialog(this, this).show();
			verifyCode = (String) SPUtils.get(this,
					mCameraInfo.getDeviceSerial(), "");
			if (verifyCode != null && verifyCode.length() != 0) {
				DataManager.getInstance().setDeviceSerialVerifyCode(
						mCameraInfo.getDeviceSerial(), verifyCode);
				if (mEZPlayer != null) {
					startRealPlay();
				}
			} else {
				VerifyCodeInput.VerifyCodeInputDialog(this, this).show();
			}
			break;
		case ErrorCode.ERROR_EXTRA_SQUARE_NO_SHARING:
		default:
			txt = Utils.getErrorTip(this, R.string.realplay_play_fail,
					errorCode);
			break;
		}

		if (!TextUtils.isEmpty(txt)) {
			setRealPlayFailUI(txt);
		} else {
			setRealPlayStopUI();
		}
	}

	/**
	 * 启动定时器
	 * 
	 * @see
	 * @since V1.0
	 */
	private void startUpdateTimer() {
		stopUpdateTimer();
		// 开始录像计时
		mUpdateTimer = new Timer();
		mUpdateTimerTask = new TimerTask() {
			@Override
			public void run() {
				if (mLandscapeTitleBar != null
						&& mRealPlayControlRl != null
						&& (mLandscapeTitleBar.getVisibility() == View.VISIBLE || mRealPlayControlRl
								.getVisibility() == View.VISIBLE)
						&& mControlDisplaySec < 5) {
					mControlDisplaySec++;
				}
				if (mRealPlayCaptureRl != null
						&& mRealPlayCaptureRl.getVisibility() == View.VISIBLE
						&& mCaptureDisplaySec < 4) {
					mCaptureDisplaySec++;
				}

				// 更新录像时间
				if (mEZPlayer != null && mIsRecording) {
					// 更新录像时间
					Calendar OSDTime = mEZPlayer.getOSDTime();
					if (OSDTime != null) {
						String playtime = Utils.OSD2Time(OSDTime);
						if (!TextUtils.equals(playtime, mRecordTime)) {
							mRecordSecond++;
							mRecordTime = playtime;
						}
					}
				}
				if (mHandler != null) {
					mHandler.sendEmptyMessage(MSG_PLAY_UI_UPDATE);
				}
			}
		};
		// 延时1000ms后执行，1000ms执行一次
		mUpdateTimer.schedule(mUpdateTimerTask, 0, 1000);
	}

	/**
	 * 停止定时器
	 * 
	 * @see
	 * @since V1.0
	 */
	private void stopUpdateTimer() {
		mCaptureDisplaySec = 4;
		updateCaptureUI();
		mHandler.removeMessages(MSG_PLAY_UI_UPDATE);
		// 停止录像计时
		if (mUpdateTimer != null) {
			mUpdateTimer.cancel();
			mUpdateTimer = null;
		}

		if (mUpdateTimerTask != null) {
			mUpdateTimerTask.cancel();
			mUpdateTimerTask = null;
		}
	}

	private void dismissPopWindow(PopupWindow popupWindow) {
		if (popupWindow != null && !isFinishing()) {
			try {
				popupWindow.dismiss();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	private void setPlayScaleUI(float scale, CustomRect oRect,
			CustomRect curRect) {
		if (scale == 1) {
			if (mPlayScale == scale) {
				return;
			}
			mRealPlayRatioTv.setVisibility(View.GONE);
			try {
				if (mEZPlayer != null) {
					mEZPlayer.setDisplayRegion(false, null, null);
				}
			} catch (BaseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			if (mPlayScale == scale) {
				try {
					if (mEZPlayer != null) {
						mEZPlayer.setDisplayRegion(true, oRect, curRect);
					}
				} catch (BaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return;
			}
			LayoutParams realPlayRatioTvLp = (LayoutParams) mRealPlayRatioTv
					.getLayoutParams();
			if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
				realPlayRatioTvLp.setMargins(Utils.dip2px(this, 10),
						Utils.dip2px(this, 10), 0, 0);
			} else {
				realPlayRatioTvLp.setMargins(Utils.dip2px(this, 70),
						Utils.dip2px(this, 20), 0, 0);
			}
			mRealPlayRatioTv.setLayoutParams(realPlayRatioTvLp);
			String sacleStr = String.valueOf(scale);
			mRealPlayRatioTv.setText(sacleStr.subSequence(0,
					Math.min(3, sacleStr.length()))
					+ "X");
			// mj mRealPlayRatioTv.setVisibility(View.VISIBLE);
			mRealPlayRatioTv.setVisibility(View.GONE);
			hideControlRlAndFullOperateBar(false);
			try {
				if (mEZPlayer != null) {
					mEZPlayer.setDisplayRegion(true, oRect, curRect);
				}
			} catch (BaseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		mPlayScale = scale;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
	 * android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.realplay_full_operate_bar:
			return true;
		default:
			break;
		}
		return false;
	}

	/**
	 * 分享 需要密码时候的弹框
	 * 
	 * @see
	 * @since V1.8.2
	 */
	private void showSharePasswordError() {
		// 处理播放密码错误
		handlePasswordError(R.string.realplay_encrypt_password_error_title,
				R.string.realplay_password_error_message4, 0);
	}

	public int getPlayStatus() {
		return mStatus;
	}

	/**
	 * 这里对方法做描述
	 * 
	 * @see
	 * @since V1.8
	 */
	private void showType() {
		if (Config.LOGGING && mEZPlayer != null) {
//			Utils.showLog(VideoPlayerActivity.this, "getType " + ",取流耗时："
//					+ (mStopTime - mStartTime));
		}
	}

	private void initLoadingUI() {
		mRealPlayLoadingRl = (RelativeLayout) findViewById(R.id.realplay_loading_rl);
		mRealPlayTipTv = (TextView) findViewById(R.id.realplay_tip_tv);
		mRealPlayPlayIv = (ImageView) findViewById(R.id.realplay_play_iv);
		mRealPlayPlayLoading = (LoadingTextView) findViewById(R.id.realplay_loading);
		mRealPlayPlayPrivacyLy = (LinearLayout) findViewById(R.id.realplay_privacy_ly);

		// 设置点击图标的监听响应函数
		mRealPlayPlayIv.setOnClickListener(this);

		mPageAnimIv = (ImageView) findViewById(R.id.realplay_page_anim_iv);
	}

	private void updateLoadingProgress(final int progress) {
		mRealPlayPlayLoading.setTag(Integer.valueOf(progress));
		mRealPlayPlayLoading.setText(progress + "%");
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				if (mRealPlayPlayLoading != null) {
					Integer tag = (Integer) mRealPlayPlayLoading.getTag();
					if (tag != null && tag.intValue() == progress) {
						Random r = new Random();
						mRealPlayPlayLoading.setText((progress + r.nextInt(20))
								+ "%");
					}
				}
			}

		}, 500);
	}

	private void setStartloading() {
		mRealPlayLoadingRl.setVisibility(View.VISIBLE);
		mRealPlayTipTv.setVisibility(View.GONE);
		mRealPlayPlayLoading.setVisibility(View.VISIBLE);
		mRealPlayPlayIv.setVisibility(View.GONE);
		mRealPlayPlayPrivacyLy.setVisibility(View.GONE);
	}

	public void setStopLoading() {
		mRealPlayLoadingRl.setVisibility(View.VISIBLE);
		mRealPlayTipTv.setVisibility(View.GONE);
		mRealPlayPlayLoading.setVisibility(View.GONE);
		mRealPlayPlayIv.setVisibility(View.VISIBLE);
		mRealPlayPlayPrivacyLy.setVisibility(View.GONE);
	}

	public void setLoadingFail(String errorStr) {
		mRealPlayLoadingRl.setVisibility(View.VISIBLE);
		mRealPlayTipTv.setVisibility(View.VISIBLE);
		mRealPlayTipTv.setText(errorStr);
		mRealPlayPlayLoading.setVisibility(View.GONE);
		mRealPlayPlayIv.setVisibility(View.GONE);
		mRealPlayPlayPrivacyLy.setVisibility(View.GONE);
	}

	private void setPrivacy() {
		mRealPlayLoadingRl.setVisibility(View.VISIBLE);
		mRealPlayTipTv.setVisibility(View.GONE);
		mRealPlayPlayLoading.setVisibility(View.GONE);
		mRealPlayPlayIv.setVisibility(View.GONE);
		mRealPlayPlayPrivacyLy.setVisibility(View.VISIBLE);
	}

	private void setLoadingSuccess() {
		mRealPlayLoadingRl.setVisibility(View.INVISIBLE);
		mRealPlayTipTv.setVisibility(View.GONE);
		mRealPlayPlayLoading.setVisibility(View.GONE);
		mRealPlayPlayIv.setVisibility(View.GONE);
	}

	@Override
	public void onInputVerifyCode(final String verifyCode) {
		LogUtil.debugLog(TAG, "verify code is " + verifyCode);
		SPUtils.put(this, mCameraInfo.getDeviceSerial(), verifyCode);
		DataManager.getInstance().setDeviceSerialVerifyCode(
				mCameraInfo.getDeviceSerial(), verifyCode);
		if (mEZPlayer != null) {
			startRealPlay();
		}
	}

	FileOutputStream mOs;

	/*
	 * 录像回调函数
	 * 
	 * @param var1:视频数据
	 * 
	 * @param var2:数据长度
	 * 
	 * @param var3:数据类型
	 * 
	 * @param var4:自定义数据
	 */
	private EZOpenSDKListener.EZStandardFlowCallback mLocalRecordCb = new EZOpenSDKListener.EZStandardFlowCallback() {
		@Override
		public void onStandardFlowCallback(int type, byte[] data, int dataLen) {
			LogUtil.verboseLog(TAG, "standard flow. type is " + type
					+ ". dataLen is " + dataLen + ". data0 is " + data[0]);

			if (mOs == null) {
				File f = new File("/sdcard/videogo.mp4");
				try {
					mOs = new FileOutputStream(f);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
					LogUtil.errorLog(TAG, "new record file failed");

					return;
				}
			}
			try {
				mOs.write(data, 0, dataLen);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {

			}
		}
	};

	
}
